package de.bazinga.rotw.input;

import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;

public class KeyLookUpAndDownAction extends KeyInputAction {
    //the camera to manipulate
    private Camera camera;

    private static final Quaternion spin = new Quaternion();
    private float m_angle = 0.0F;
    private final Vector3f m_tmp = new Vector3f();
    
    /**
     * Constructor instantiates a new <code>KeyLookUpAction</code> object.
     * 
     * @param camera
     *            the camera to tilt.
     * @param speed
     *            the speed at which the camera tilts.
     */
    public KeyLookUpAndDownAction(Camera camera, float speed) {
    	super();
        this.camera = camera;
        this.speed = speed;
    }

    /**
     * <code>performAction</code> adjusts the view of the camera to tilt up a
     * given angle. This angle is determined by the camera's speed and the time
     * which has passed.
     * 
     * @see com.jme.input.action.KeyInputAction#performAction(InputActionEvent)
     */
    public void performAction(InputActionEvent evt) {
    	m_angle -= speed * evt.getTime();
    	// Set Maximum/Minimum to 90 degrees
        m_angle = Math.max(m_angle, -FastMath.HALF_PI + 0.01F);
        m_angle = Math.min(m_angle, FastMath.HALF_PI - 0.01F);
        
        m_tmp.set(camera.getDirection()).setY(0.0F);
        m_tmp.normalizeLocal();
        m_tmp.set(m_tmp.z, 0.0F, -m_tmp.x);
        spin.fromAngleNormalAxis(m_angle, m_tmp);
        m_tmp.set(camera.getDirection()).setY(0.0F);
        m_tmp.normalizeLocal();
        spin.multLocal(m_tmp);
        camera.lookAt(m_tmp.addLocal(camera.getLocation()), Vector3f.UNIT_Y);
        camera.normalize();
        camera.update();
    }
}